from ete4 import Tree

t = Tree('((H:0.3,I:0.1):0.5, A:1, (B:0.4,(C:1,(J:1, (F:1, D:1):0.5):0.5):0.5):0.5);')
#  ╭─┬╴H
# ─┤ ╰╴I
#  ├╴A
#  ╰─┬╴B
#    ╰─┬╴C
#      ╰─┬╴J
#        ╰─┬╴F
#          ╰╴D

# Get the node D in a very simple way.
D = t['D']

# Get the path from D to the root.
node = D
path = []
while node.up:
    path.append(node)
    node = node.up

# I substract D node from the total number of visited nodes
print('There are', len(path)-1, 'nodes between D and the root')

# A simpler way would be:
path = list(D.ancestors())
print('There are', len(path)-1, 'nodes between D and the root')


# Using parentheses you can use by-operand search syntax as a node
# instance itself
Csparent= t['C'].up
Bsparent= t['B'].up
Jsparent= t['J'].up

# I check if nodes belong to certain partitions.
print("It is", Csparent in Bsparent, "that C's parent is under B's ancestor")
print("It is", Csparent in Jsparent, "that C's parent is under J's ancestor")
